package com.medishare.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import android.util.Log;

public class HttpUtils{

	static public String getReponseByHttpGet(String GetURL,int TimeOut) {
		BufferedReader in = null;
        StringBuffer sb = new StringBuffer("");
        try{
        	HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams, TimeOut);
            HttpConnectionParams.setSoTimeout(httpParams, TimeOut);
            HttpClient httpClient = new DefaultHttpClient(httpParams) ;
            HttpGet request = new HttpGet(GetURL);
            HttpResponse response = httpClient.execute(request);
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String line = "";
            String NL = System.getProperty("line.separator");
            while((line = in.readLine()) != null){
                sb.append(line + NL);
            }
            in.close();
        }catch(Exception e){
        	Log.e("exception", e.toString());
            return "timeout";
        } finally{
            if(in != null){
                try{
                    in.close();
                }catch(IOException ioe){
//                    Log.e(TAG, ioe.toString());
                }
            }
        }
        String page = sb.toString();

    	return page;
    }

	public static String getHttpContext(HttpResponse response){
		String context="";
		BufferedReader in = null;
		StringBuffer sb = new StringBuffer("");
		try {
			in = new BufferedReader(new InputStreamReader(response.getEntity()
					.getContent()));
			String line = "";
			String NL = System.getProperty("line.separator");

			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
			in.close();
		} catch (IllegalStateException e) {
			e.printStackTrace();
			return "fail";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "fail";
		}finally {
			if(in != null) {
				try {
					in.close();
	            }catch(IOException ioe) {
	            }
	        }
	    }
		context=sb.toString();
//		Log.e("context", "context:"+context);
		return context;
	}

	public static String getReponseByHttpPost(String url, JSONObject obj){
		try{
			HttpPost mPost = new HttpPost(url);
			mPost.setEntity(new StringEntity(obj.toString(),HTTP.UTF_8));
			BasicHttpParams localBasicHttpParams1 = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(localBasicHttpParams1, 20000);
			HttpConnectionParams.setSoTimeout(localBasicHttpParams1, 20000);
			DefaultHttpClient mHttpClient = new DefaultHttpClient(localBasicHttpParams1);
			HttpResponse response = mHttpClient.execute(mPost);
			Log.i("tag", response.getStatusLine().getStatusCode() + "");
			if(response.getStatusLine().getStatusCode() ==200){			
				return EntityUtils.toString(response.getEntity());
			}else{
				return "fail";
			}
		}catch (UnsupportedEncodingException e){
			e.printStackTrace();
		} catch (ClientProtocolException e){
			e.printStackTrace();
		} catch (IOException e){
			e.printStackTrace();
		}
			return "exception";
	}
	
}
